/* 
Creates the figures for the analysis of the CUT reform in the amenities sample
	// input: National_boards_centrals_collapsed; amenities_gender_sample; estab_union_act; filledpanel; estabtreat_forRAIS_singest_deid; estabchars_wide; union_boards_panel_20112019
	// output: firststage_national_boards_treat; firststage_national_boards_centrals; 
				pagerankdiff_femclause_sectoral; pagerankdiff_femclause_sectoral_DD; 
				number_of_clauses_bysharewomen; number_of_clauses_bysharewomen_DD; 
				any_female_cl_DD_did_DD; female_cl_DD_sh_did_DD; female_clauses_DD_b_did_DD; 
				female_clauses_DD_did_DD; male_cl_DD_sh_did_DD; male_clauses_DD_did_DD; 
				female_clauses_DD_het_by_femxshare_DD; female_cl_DD_sh_het_by_femxshare_DD; 
				female_clauses_DD_het_by_femishare_DD; female_cl_DD_sh_het_by_femishare_DD;
				female_clauses_DD_FP; female_clauses_DD; male_clauses_DD_FP; female_male_DD_FP;
				shfem_unionboards_did_restr; womPorVP_unionboards_did_restr
*/ 

cap log close
cap log using "$logs/amenities_figures_log", replace


********************************************************
*** GENDER PARITY IN NATIONAL LEADERSHIP BY UNON CENTRAL
********************************************************

* data toggles
local CBAbefore = 2020 //2015: must negotiate CBA prior to reform; 2020: ignore restriction
local DDmethod = "base" //base: baseline method; any: clause selected in any method; all: clause selected in all methods
local geoglevel = "state_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local clstr = "employer_id" //estab_union_pair; employer_id; union_id
local cov2014 = 0 //0: allow for no CBA coverage in 2014; 1: ensure CBA coverage in 2014 
local filled = 1 //0: use new contracts panel; 1: used filled panel

use "$files/National_boards_centrals_collapsed", clear
gen CUT = (central==5)
rename start_year year 

// we are missing one union central in 2012 and 2013: INTERSINDICAL was founded in 2014. So just consider average of other 7
drop if central == 7 

* Figure: CUT vs. average of other centrals
preserve
	collapse (mean) female, by(CUT year)
	keep if inrange(year, 2012, 2019)

	twoway (scatter female year if CUT==1, connect(direct) lc(sourapple1) mc(sourapple1)) ///
		(scatter female year if CUT==0, connect(direct) lc(sourapple3) ms(S) mc(sourapple3)), ///
		ytitle("Share of women in national board", size(medlarge)) yla(,labsize(medium)) ///
		xtitle("") xsc(r(2012 2019)) xlab(2012(1)2019, labsize(medium)) xline(2014.5,lp(dash) lc(sourapple4)) ///
		legend(order(1 "CUT" 2 "Average of other centrals") size(medium) region(lstyle(none) color(none))) ///
		plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white))
			
	graph export "$figures/firststage_national_boards_treat.tif", replace		
restore

* Figure: by union central
preserve
	collapse (mean) female, by(central year)
	keep if inrange(year, 2012, 2019)

	twoway (scatter female year if central==5, connect(direct) lc(sourapple1) mc(sourapple1)) ///
		(scatter female year if central==2, connect(direct) lc(sourapple2) ms(S) mc(sourapple2)) ///
		(scatter female year if central==4, connect(direct) lc(sourapple3) ms(T) mc(sourapple3)) ///
		(scatter female year if central==3, connect(direct) lc(sourapple4) ms(D) mc(sourapple4)) ///
		(scatter female year if central==6, connect(direct) lc(sourapple1) lp(longdash) mc(sourapple1)) ///
		(scatter female year if central==8, connect(direct) lc(sourapple2) lp(longdash) ms(S) mc(sourapple2)) ///
		(scatter female year if central==9, connect(direct) lc(sourapple3) lp(longdash) ms(T) mc(sourapple3)) ///
		(scatter female year if central==1, connect(direct) lc(sourapple4) lp(longdash) ms(D) mc(sourapple4)), ///
		ytitle("Share of women in national board", size(medlarge)) yla(,labsize(medium)) ///
		ylabel(0.1(0.1)0.6, gmin gmax) ///
		xtitle("") xsc(r(2012 2019)) xlab(2012(1)2019, labsize(medium)) xline(2014.5,lp(dash) lc(sourapple4)) ///
		legend(order(1 "CUT" 2 "Conlutas" 3 "CTB" 4 "CGTB" 5 "FS" 6 "NCST" 7 "UGT" 8 "CSB") size(medium) rows(2) region(lstyle(none) color(none))) ///
		plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white))
			
	graph export "$figures/firststage_national_boards_centrals.tif", replace		
restore


*******************************************************
*** SENSE CHECKS FOR FEMALE- AND MALE-CENTRIC AMENITIES
*******************************************************

* data toggles
local CBAbefore = 2020 //2015: must negotiate CBA prior to reform; 2020: ignore restriction
local DDmethod = "base" //base: baseline method; any: clause selected in any method; all: clause selected in all methods
local geoglevel = "state_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local clstr = "employer_id" //estab_union_pair; employer_id; union_id
local cov2014 = 0 //0: allow for no CBA coverage in 2014; 1: ensure CBA coverage in 2014 
local filled = 1 //0: use new contracts panel; 1: used filled panel

	use "$files/amenities_gender_sample", clear

	* PageRank normalizations	
	gen fe_diff = abs(fe_m-fe_f)
	gen pagerank_diff = abs(pagerank_m-pagerank_f)
	qui sum fe_diff, d
	list estabid fe_m fe_f fe_diff pagerank_m pagerank_f pagerank_diff if fe_diff==r(min)
	qui sum pagerank_diff if fe_diff==r(min)
	list estabid fe_m fe_f fe_diff pagerank_m pagerank_f pagerank_diff if pagerank_diff==r(min)
	gen Diff = pagerank_diff if pagerank_diff==r(min)
	gen X = pagerank_m if pagerank_diff==r(min)
	gen Ratio = pagerank_m/pagerank_f if pagerank_diff==r(min)
	gen B = Diff/(X*(Ratio-1))
	gen A = (Diff/X)+B
	gen pagerank_m_norm = pagerank_m
	sum Ratio
	gen pagerank_f_norm = pagerank_f*r(mean)
	
	* PageRanks: logs of exp(V)
	gen ln_pagerank_m = log(pagerank_m)
	gen ln_pagerank_f = log(pagerank_f)
	gen ln_pagerank_m_norm = log(pagerank_m_norm)
	gen ln_pagerank_f_norm = log(pagerank_f_norm)

	* Inverse hyporbolic sine of clauses
	foreach x of varlist *cl_* {
		replace `x' = asinh(`x')
	}

	* Restrictions
	//keep estab with at least 4 sectoral contracts over time frame considered
	egen tag_estabid = tag(estabid)
	sum cnt, d
	tab cnt if tag_estabid==1
	keep if cnt>=4

	// gap in page ranks (with and without normalization)
	gen gap_pagerank = ln_pagerank_f - ln_pagerank_m
	gen gap_pagerank_norm = ln_pagerank_f_norm - ln_pagerank_m_norm
	// index ranking of PageRanks (minimum at 0; maximum at 100)
	qui sum pagerank_m
	gen pagerank_m_index = (pagerank_m-r(min))*(100/(r(max)-r(min)))
	qui sum pagerank_f
	gen pagerank_f_index = (pagerank_f-r(min))*(100/(r(max)-r(min)))
	gen gap_pagerank_index = pagerank_f_index - pagerank_m_index
	//correlation of gaps
	corr gap_pagerank* 

	* Figure: Value gaps and intuitive female clauses
	//using gap_pagerank_norm simply shifts along the y-axis
	qui reg gap_pagerank female_clauses
	local beta = _b[female_clauses]
	local se = _se[female_clauses]
	local beta : di %04.3f `beta'
	local se : di %04.3f `se'
	binscatter gap_pagerank female_clauses, ///
		xtitle("Female clauses (intuitive)", size(medlarge)) xla(,labsize(medium)) ///
		ytitle("PageRank gender gap (female - male)", size(medlarge)) ///
		ylabel(0(0.2)0.8, labsize(medium) gmin gmax) /// 
		line(lfit) lc(sourapple4) mcolor(sourapple1) reportreg ///	
		text(0.2 8 "`beta'" "(`se')", color(sourapple1) size(medium)) ///
		plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white))
		
	graph export "$figures/pagerankdiff_femclause_sectoral.tif", replace

	* Figure: Value gaps and data-driven female clauses
	//using gap_pagerank_norm simply shifts along the y-axis
	if "`DDmethod'"=="base" {
		#d ; 
			egen female_clauses_DD = rowtotal(
				cl_23aux_cre
				cl_51fal
				cl_72lic_ado
				cl_73out_dis
				cl_22adi_tem
				cl_43est_mae
				cl_43est_abo
				cl_72lic_rem
				cl_22adi_not
				cl_43est_por
				cl_72lic_abo
				cl_43pol_par
				cl_51pro_red
				cl_81gar_dir
				cl_91ren_res
				cl_62exa_med
				cl_82sin_cam
				cl_62cam_edu
				cl_82dir_opo
				cl_11rea_cor
			);
			#d cr 
		}
	if "`DDmethod'"=="any" {
		#d ; 
			egen female_clauses_DD = rowtotal(
				cl_23aux_cre
				cl_51fal
				cl_72lic_ado
				cl_73out_dis
				cl_22adi_tem
				cl_43est_mae
				cl_72lic_rem
				cl_22adi_not
				cl_72lic_abo
				cl_43pol_par
				cl_91ren_res
				cl_43est_por
				cl_51pro_red
				cl_82sin_cam
				cl_62exa_med
				cl_82dir_opo
				cl_11rea_cor
				cl_43est_abo
				cl_43est_ado
				cl_81gar_dir
				cl_62cam_edu
				cl_43est_ser
				cl_31des_dem
				cl_43out_est
				cl_24pre
				cl_42ass_mor
				cl_72lic_mat
			);
			#d cr 
		}
	if "`DDmethod'"=="all" {
		#d ; 
			egen female_clauses_DD = rowtotal(
				cl_23aux_cre
				cl_51fal
				cl_72lic_ado
				cl_73out_dis
				cl_22adi_tem
				cl_43est_mae
				cl_72lic_rem
				cl_22adi_not
				cl_72lic_abo
				cl_43pol_par
				cl_91ren_res
				cl_11rea_cor
			);
			#d cr 
		}

		qui reg gap_pagerank female_clauses_DD
		local beta = _b[female_clauses_DD]
		local se = _se[female_clauses_DD]
		local beta : di %04.3f `beta'
		local se : di %04.3f `se'
		binscatter gap_pagerank female_clauses_DD, ///
			xtitle("Female clauses (data driven)", size(medlarge)) xla(,labsize(medium)) ///
			ytitle("PageRank gender gap (female - male)", size(medlarge)) ///
			ylabel(0(0.2)0.8, labsize(medium) gmin gmax) ///
			line(lfit) lc(sourapple4) mcolor(sourapple1) reportreg ///
			text(0.2 8 "`beta'" "(`se')", color(sourapple1) size(medium)) ///
			plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white))
			
		graph export "$figures/pagerankdiff_femclause_sectoral_DD.tif", replace
		
	* Figure: intuitive female clauses and share of women
		if `filled' {
			use "$files/filledpanel.dta", clear
		}
		else{
			use "$files/estab_union_act.dta", clear
		}
		keep if ind_mode!=. & microregion_mode!=.
		keep if treat!=. 
		keep if inrange(year, 2011, 2017)	
		keep if min_cbayear<`CBAbefore'
		if `no2011' {
			drop if year==2011
		}
		if `cov2014' {
			gen xxx = (total_clauses==0)&(year==2014)
			egen nocover_bl = max(xxx), by(estab_union_pair)
			unique estab_union_pair
			unique estab_union_pair if nocover_bl==0
			keep if nocover_bl==0
			drop nocover_bl xxx
		}
		gen female_clauses_DD = female_clauses_DD`DDmethod'
		gen male_clauses_DD = male_clauses_DD`DDmethod'
			
		* merge with estab characteristics
		merge m:1 employer_id using "$files/estabchars_wide"
		drop if _merge == 2
		drop _merge
		forvalues y = 2011/2017 {
			replace tot_fememployment`y' = 0 if tot_fememployment`y' == . & tot_employment`y' !=.
			gen sharefemale_`y' = tot_fememployment`y'/tot_employment`y'
		}
		
		* Sample restrictions
		keep if year == 2014
		gen size = 1
		gen sh_female_bl_round = round(sharefemale_2014, 0.05)
		
		preserve
			collapse  (mean) female_clauses (sum) size , by(sh_female_bl_round)
			
		* Graph
			scatter female_clauses sh_female_bl_round [aw=size], ///
				msymbol(Oh) mcolor(sourapple1) ///
				xtitle("Share of women at establishment", size(medlarge)) xla(, labsize(medium)) ///
				ytitle("Mean clause count (female; intuitive)", size(medlarge)) yla(0.8(0.1)1.2, labsize(medium) gmax gmin) ///
				xline(0.5,lp(dash) lc(sourapple4)) ///
				plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white))
				
			graph export "$figures/number_of_clauses_bysharewomen.tif", replace
			
		* Graph (with linear fit)
			reg female_clauses sh_female_bl_round, r
			local y_int = _b[_cons]
			local beta = _b[sh_female_bl_round]
			local se = _se[sh_female_bl_round]
			local y_int : di %04.3f `y_int'
			local beta : di %04.3f `beta'
			local se : di %04.3f `se'
			twoway (scatter female_clauses sh_female_bl_round [aw=size], msymbol(Oh) mcolor(sourapple1)) ///
				(lfit female_clauses sh_female_bl_round, lc(sourapple1)), ///
				xtitle("Share of women at establishment", size(medlarge)) xla(, labsize(medium)) ///
				ytitle("Mean clause count (female; intuitive)", size(medlarge)) yla(0.8(0.1)1.2, labsize(medium) gmax gmin) ///
				xline(0.5,lp(dash) lc(sourapple4)) ///
				legend(off) ///
				note("y-intercept = `y_int'; slope = `beta' (`se')") ///
				plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white))
				
			graph export "$figures/number_of_clauses_bysharewomen_alt.tif", replace
			
		restore

	* Figure: Data-driven clauses and share of women
	
		* Binscatter graphs
		qui reg female_clauses_DD sharefemale_2014, r
		local y_int = _b[_cons]
		local beta = _b[sharefemale_2014]
		local se = _se[sharefemale_2014]
		local y_int : di %04.3f `y_int'
		local beta : di %04.3f `beta'
		local se : di %04.3f `se'
		
		gen above50 = (sharefemale_2014>0.5)
		gen int50 = sharefemale_2014 * above50
		reg female_clauses_DD sharefemale_2014 int50, r
		reg male_clauses_DD sharefemale_2014, r
		
		preserve
		collapse  (mean) female_clauses_DD male_clauses_DD (sum) size , by(sh_female_bl_round)
		
		* Graph
		twoway 	(scatter female_clauses_DD sh_female_bl_round [aw=size], msymbol(Oh) mcolor(sourapple1)) ///
				(scatter male_clauses_DD sh_female_bl_round [aw=size], msymbol(Sh) mcolor(sourapple3)) , ///
				xtitle("Share of women at establishment", size(medlarge)) xla(, labsize(medium)) ///
				ytitle("Mean clause count (data driven)", size(medlarge)) yla(, labsize(medium)) ///
				xline(0.5,lp(dash) lc(sourapple4)) ///
				plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white)) ///
				legend(ring(0) pos(1) cols(1) size(medium) ///
				region(lstyle(none) color(none)) order(1 "Female clauses" 2 "Male clauses"))
				
		graph export "$figures/number_of_clauses_bysharewomen_DD.tif", replace
		
		* Graph (with linear fit)
		reg female_clauses_DD sh_female_bl_round, r
		local y_int = _b[_cons]
		local beta = _b[sh_female_bl_round]
		local se = _se[sh_female_bl_round]
		local y_int : di %04.3f `y_int'
		local beta : di %04.3f `beta'
		local se : di %04.3f `se'
		reg male_clauses_DD sh_female_bl_round, r
		local y_intm = _b[_cons]
		local betam = _b[sh_female_bl_round]
		local sem = _se[sh_female_bl_round]
		local y_intm : di %04.3f `y_intm'
		local betam : di %04.3f `betam'
		local sem : di %04.3f `sem'
		twoway 	(scatter female_clauses_DD sh_female_bl_round [aw=size], msymbol(Oh) mcolor(sourapple1)) ///
				(scatter male_clauses_DD sh_female_bl_round [aw=size], msymbol(Sh) mcolor(sourapple3)) ///
				(lfit female_clauses_DD sh_female_bl_round, lc(sourapple1)) ///
				(lfit male_clauses_DD sh_female_bl_round, lc(sourapple3)), ///
				xtitle("Share of women at establishment", size(medlarge)) xla(, labsize(medium)) ///
				ytitle("Mean clause count (data driven)", size(medlarge)) yla(, labsize(medium)) ///
				xline(0.5,lp(dash) lc(sourapple4)) ///
				legend(ring(0) pos(1) cols(1) size(medium) ///
				region(lstyle(none) color(none)) order(1 "Female clauses" 2 "Male clauses")) ///
				note("Female: y-intercept = `y_int'; slope = `beta' (`se')" "Male: y-intercept = `y_intm'; slope = `betam' (`sem')") ///
				plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white))
				
		graph export "$figures/number_of_clauses_bysharewomen_DD_alt.tif", replace
		
		restore
		

**********************************************************************
*** EFFECT OF THE CUT REFORM ON FEMALE- AND MALE-CENTRIC AMENITIES ***
**********************************************************************

* data toggles
local CBAbefore = 2020 //2015: must negotiate CBA prior to reform; 2020: ignore restriction
local DDmethod = "base" //base: baseline method; any: clause selected in any method; all: clause selected in all methods
local geoglevel = "state_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local clstr = "employer_id" //estab_union_pair; employer_id; union_id
local cov2014 = 0 //0: allow for no CBA coverage in 2014; 1: ensure CBA coverage in 2014 
local filled = 1 //0: use new contracts panel; 1: used filled panel


	if `filled' {
		use "$files/filledpanel.dta", clear
	}
	else{
		use "$files/estab_union_act.dta", clear
	}
	keep if ind_mode!=. & microregion_mode!=.
	keep if treat!=. 
	keep if inrange(year, 2011, 2017)	
	keep if min_cbayear<`CBAbefore'
	if `cov2014' {
		gen xxx = (total_clauses==0)&(year==2014)
		egen nocover_bl = max(xxx), by(estab_union_pair)
		unique estab_union_pair
		unique estab_union_pair if nocover_bl==0
		keep if nocover_bl==0
		drop nocover_bl xxx
	}
	
	* data driven method
	gen female_clauses_DD = female_clauses_DD`DDmethod'
	gen male_clauses_DD = male_clauses_DD`DDmethod'
	gen female_clauses_DD_b = female_clauses_DD`DDmethod'_b
	gen male_clauses_DD_b = male_clauses_DD`DDmethod'_b
	gen female_cl_DD_sh = female_cl_DD`DDmethod'_sh
	gen male_cl_DD_sh = male_cl_DD`DDmethod'_sh
	gen female_cl_DD_sh_b = female_cl_DD`DDmethod'_sh_b
	gen male_cl_DD_sh_b = male_cl_DD`DDmethod'_sh_b
	gen any_female_cl_DD = any_female_cl_DD`DDmethod'
	gen any_male_cl_DD = any_male_cl_DD`DDmethod'
	
	* time varying fixed effects
	egen indyrFE = group(year ind2d_num)
	egen stateind = group(state_mode ind2d_num)
	egen microind = group(microregion_mode ind2d_num)
	egen geogyrFE = group(year `geoglevel')
	if "`geoglevel'"=="stateind" | "`geoglevel'"=="microind" {
		drop indyrFE
	}
	
	* for clustering
	egen clustergrp = group(`clstr')
	
	* Event study var 
	tab year, gen(yearj)
		
		forvalues i = 1/7 {
			gen interj`i'=treat*yearj`i'
		}
		
		lab var interj1 "2011"
		lab var interj2 "2012"
		lab var interj3 "2013"
		lab var interj4 "2014"
		lab var interj5 "2015"
		lab var interj6 "2016"
		lab var interj7 "2017"
		
		gen zero=0
		lab var zero "2014"
		replace interj4 = zero
		
	* Labels 
	label var female_clauses_DD 	"Female clauses (intensive; data driven)"
	label var male_clauses_DD 		"Male clauses (intensive; data driven)"
	label var female_clauses_DD_b 	"Female clauses (binary; data driven)"
	label var male_clauses_DD_b 	"Male clauses (binary; data driven)"
	label var any_female_cl_DD 		"Female clauses (extensive; data driven)"
	label var any_male_cl_DD 		"Male clauses (extensive; data driven)"
	label var female_cl_DD_sh 		"Female clauses (share; data driven)"
	label var male_cl_DD_sh 		"Male clauses (share; data driven)"
	
		
		if `no2011' {
			drop if year==2011
			drop interj1
			local xl = 3.5
		}
		else {
			local xl = 4.5
		}

		* Intensive margin 
		foreach var in female_clauses_DD male_clauses_DD   {
		    
		    qui reghdfe `var' treatpost, absorb(estab_union_pair *FE) cluster(clustergrp)
			local coeff = _b[treatpost]
			local coeff : di %04.3f `coeff'
			local serr = _se[treatpost]
			local serr : di %04.3f `serr'
			
			reghdfe `var' interj*, absorb(estab_union_pair *FE) cluster(clustergrp)
			estimates store `var'
			
			
			#d ;
			coefplot (`var' ,connect(l) lpatt(solid)lcol(sourapple1)
			mcol(sourapple1)ciopts(lpatt(solid)lcol(sourapple1%50))
			text(0.6 4 "`coeff'" "(`serr')", color(sourapple1) size(medium) placement(se))), 
			omitted keep( interj* ) 
			yline(0, lc(sourapple4) lp(solid)) 
			xline(`xl', lc(sourapple4) lp(dash)) 
			vertical ytitle("`: var label `var''", size(medlarge) height(5)) 
			ylabel(-0.2(0.2)0.8, labsize(medium) gmin gmax) xla(, labsize(medium))
			plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white)) ;
			#d cr 
			 
			graph export "$figures/`var'_did_DD.tif", replace
			
		}
		
		* Extensive margin 
		foreach var in any_female_cl_DD  {
		    
		    qui reghdfe `var' treatpost, absorb(estab_union_pair *FE) cluster(clustergrp)
			local coeff = _b[treatpost]
			local coeff : di %04.3f `coeff'
			local serr = _se[treatpost]
			local serr : di %04.3f `serr'
			
			reghdfe `var' interj*, absorb(estab_union_pair *FE) cluster(clustergrp)
			estimates store `var'
			
			
			#d ;
			coefplot (`var' ,connect(l) lpatt(solid)lcol(sourapple1)
			mcol(sourapple1)ciopts(lpatt(solid)lcol(sourapple1%50))
			text(0.02 5 "`coeff'" "(`serr')", color(sourapple1) size(medium) placement(se))), 
			omitted keep( interj* ) 
			yline(0, lc(sourapple4) lp(solid)) 
			xline(`xl', lc(sourapple4) lp(dash)) 
			vertical ytitle("`: var label `var''", size(medlarge) height(5)) 
			ylabel(, labsize(medium)) xlabel(, labsize(medium)) 
			plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white)) ;
			#d cr 
			 
			graph export "$figures/`var'_did_DD.tif", replace
			
		}
		
		* Intensive margin (binary)
		foreach var in female_clauses_DD_b  {
		    
		    qui reghdfe `var' treatpost, absorb(estab_union_pair *FE) cluster(clustergrp)
			local coeff = _b[treatpost]
			local coeff : di %04.3f `coeff'
			local serr = _se[treatpost]
			local serr : di %04.3f `serr'
			
			reghdfe `var' interj*, absorb(estab_union_pair *FE) cluster(clustergrp)
			estimates store `var'
			
			
			#d ;
			coefplot (`var' ,connect(l) lpatt(solid)lcol(sourapple1)
			mcol(sourapple1)ciopts(lpatt(solid)lcol(sourapple1%50))
			text(0.6 4 "`coeff'" "(`serr')", color(sourapple1) size(medium) placement(se))), 
			omitted keep( interj* ) 
			yline(0, lc(sourapple4) lp(solid)) 
			xline(`xl', lc(sourapple4) lp(dash)) 
			vertical ytitle("`: var label `var''", size(medlarge) height(5)) 
			ylabel(-0.2(0.2)0.8, labsize(medium) gmin gmax) xla(, labsize(medium))
			plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white)) ;
			#d cr 
			 
			graph export "$figures/`var'_did_DD.tif", replace
			
		}
		
		* Shares margin
		foreach var in female_cl_DD_sh male_cl_DD_sh   {
		    
		    qui reghdfe `var' treatpost, absorb(estab_union_pair *FE) cluster(clustergrp)
			local coeff = _b[treatpost]
			local coeff : di %04.3f `coeff'
			local serr = _se[treatpost]
			local serr : di %04.3f `serr'
			
			reghdfe `var' interj*, absorb(estab_union_pair *FE) cluster(clustergrp)
			estimates store `var'
			
			
			#d ;
			coefplot (`var' ,connect(l) lpatt(solid)lcol(sourapple1)
			mcol(sourapple1)ciopts(lpatt(solid)lcol(sourapple1%50))
			text(0.02 5 "`coeff'" "(`serr')", color(sourapple1) size(medium) placement(se))), 
			omitted keep( interj* ) 
			yline(0, lc(sourapple4) lp(solid)) 
			xline(3.5, lc(sourapple4) lp(dash)) 
			vertical ytitle("`: var label `var''", size(medlarge) height(5)) 
			ylabel(-0.01(0.01)0.03, labsize(medium) gmin gmax) xla(, labsize(medium))
			plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white)) ;
			#d cr 
			 
			graph export "$figures/`var'_did_DD.tif", replace
			
		}
		

************************************************************************************
*** EFFECT ON AMENITIES BY SHARE OF FEMALE WORKERS AT ESTABLISHMENT ****************
*** EFFECT ON AMENITIES BY LOW REPRESENTATION OF WOMEN IN THE WORKPLACE VS UNION ***
************************************************************************************

* data toggles
local CBAbefore = 2020 //2015: must negotiate CBA prior to reform; 2020: ignore restriction
local DDmethod = "base" //base: baseline method; any: clause selected in any method; all: clause selected in all methods
local geoglevel = "state_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local clstr = "employer_id" //estab_union_pair; employer_id; union_id
local cov2014 = 0 //0: allow for no CBA coverage in 2014; 1: ensure CBA coverage in 2014 
local filled = 1 //0: use new contracts panel; 1: used filled panel


		use "$files\estabtreat_forRAIS_singest_deid.dta", clear
		keep fakeid_estab singest  
		duplicates drop
		tempfile singestab 
		save `singestab'
		
		use "$files/unionboards_SDSR_clean.dta", clear
		keep union_id sh_female_bl female_president_bl female_vicepresident_bl female_treasurer_bl female_secgen_bl union_type
		//tag unions with info on boards at baseline 
		duplicates drop
		egen female_PorVP = rowmax(female_president_bl female_vicepresident_bl)
		egen female_PorVPorSG = rowmax(female_president_bl female_vicepresident_bl female_secgen_bl)
		egen female_PorVPorSGorT = rowmax(female_president_bl female_vicepresident_bl female_secgen_bl female_treasurer_bl)
		tempfile baselines
		save `baselines'
		
		if `filled' {
			use "$files/filledpanel.dta", clear
		}
		else{
			use "$files/estab_union_act.dta", clear
		}
		keep if ind_mode!=. & microregion_mode!=.
		keep if treat!=. 
		keep if inrange(year, 2011, 2017)	
		keep if min_cbayear<`CBAbefore'
		if `no2011' {
			drop if year==2011
		}
		if `cov2014' {
			gen xxx = (total_clauses==0)&(year==2014)
			egen nocover_bl = max(xxx), by(estab_union_pair)
			unique estab_union_pair
			unique estab_union_pair if nocover_bl==0
			keep if nocover_bl==0
			drop nocover_bl xxx
		}
	
		* data driven method
		gen female_clauses_DD = female_clauses_DD`DDmethod'
		gen male_clauses_DD = male_clauses_DD`DDmethod'
		gen female_clauses_DD_b = female_clauses_DD`DDmethod'_b
		gen male_clauses_DD_b = male_clauses_DD`DDmethod'_b
		gen female_cl_DD_sh = female_cl_DD`DDmethod'_sh
		gen male_cl_DD_sh = male_cl_DD`DDmethod'_sh
		gen female_cl_DD_sh_b = female_cl_DD`DDmethod'_sh_b
		gen male_cl_DD_sh_b = male_cl_DD`DDmethod'_sh_b
		gen female_cl_DD_any = any_female_cl_DD`DDmethod'
		gen male_cl_DD_any = any_male_cl_DD`DDmethod'
		
		* merge with estab characteristics
		merge m:1 employer_id using "$files/estabchars_wide"
		drop if _merge == 2
		drop _merge
		
		merge m:1 fakeid_estab using `singestab'
		drop if _merge == 2
		replace singest = 0 if singest==.
		drop _merge
		
		*merge with union characteristics
		cap drop _merge
		merge m:1 union_id  using `baselines' , keep(1 3)
		drop _merge
		
		* time varying fixed effects
		egen indyrFE = group(year ind2d_num)
		egen stateind = group(state_mode ind2d_num)
		egen microind = group(microregion_mode ind2d_num)
		egen geogyrFE = group(year `geoglevel')
		if "`geoglevel'"=="stateind" | "`geoglevel'"=="microind" {
			drop indyrFE
		}
		
		* for clustering
		egen clustergrp = group(`clstr')	
		
		/*** Share of women bins ***/
		egen unique_obs = tag(estab_union_pair)
		
		/* Baseline share of women */
		forvalues y = 2011/2017 {
			replace tot_fememployment`y' = 0 if tot_fememployment`y' == . & tot_employment`y' !=.
			gen sharefemale_`y' = tot_fememployment`y'/tot_employment`y'
		}
		cap  drop temp
		egen sharewomen_bl = max(sharefemale_2014), by(employer_id)
		mdesc sharewomen_bl
		
		/* Baseline % women bins (establishment) */
		forvalues i = 0/4 {
		local j1=`i'*0.2
		local j2=`i'*0.2+0.2
		gen share`i'=(sharewomen_bl>=`j1' & sharewomen_bl<`j2')
		}
		replace share4 = 1 if sharewomen_bl==1
		
		qui count if unique_obs==1
		local denom = r(N)
		forvalues i = 0/4 {
		qui count if unique_obs==1 & share`i'==1
		local numer = r(N)
		di "% in share`i' = " (`numer'/`denom')*100
		}
		
		/* Adjust bins: Baseline % women bins (establishment) */
		forvalues i = 0/4 {
		gen xshare`i' = share`i'
		}
		replace xshare3 = 1 if xshare4==1
		drop xshare4
		
		qui count if unique_obs==1
		local denom = r(N)
		forvalues i = 0/3 {
		qui count if unique_obs==1 & xshare`i'==1
		local numer = r(N)
		di "% in share`i' = " (`numer'/`denom')*100
		}
		qui unique estab_union_pair
		local denom = `r(unique)'
		forvalues i = 0/3 {
			qui unique estab_union_pair if xshare`i'==1
			local numer = `r(unique)'
			di `numer'/`denom'*100
		}
		qui unique employer_id
		local denom = `r(unique)'
		forvalues i = 0/3 {
			qui unique employer_id if xshare`i'==1
			local numer = `r(unique)'
			di `numer'/`denom'*100
		}
		
		/* Interaction: % women median (establishment vs. unions)*/
		gen ishare0 = 1
		gen ishare1 = (sharewomen_bl>=0 & sharewomen_bl<0.33)&(sh_female_bl>=0 & sh_female_bl<0.33)
		gen ishare2 = (sharewomen_bl>=0 & sharewomen_bl<0.33)&(sh_female_bl>=0.33 & sh_female_bl<=1)
		gen ishare3 = (sharewomen_bl>=0.33 & sharewomen_bl<=1)&(sh_female_bl>=0 & sh_female_bl<0.33)
		gen ishare4 = (sharewomen_bl>=0.33 & sharewomen_bl<=1)&(sh_female_bl>=0.33 & sh_female_bl<=1)
		
		
		/*** Heterogeneity plot ***/
		* Plot heterogeneity in treatment effect by share of women at the firm 
		
		local depvar _clauses_DD _cl_DD_sh
		local j=1
		
		foreach var of local depvar {
		
			if `j'==1 {
				local margin = "intensive"
			}
			if `j'==2 {
				local margin = "share"
			}

			forvalues i = 0/3 {
				eststo DDf`i': reghdfe female`var' treatpost if xshare`i'==1, absorb(estab_union_pair *FE) cluster(clustergrp)
				eststo DDm`i': reghdfe male`var' treatpost if xshare`i'==1, absorb(estab_union_pair *FE) cluster(clustergrp)
			}
			
			#delimit ;
			coefplot  (DDf0, aseq("0-19") \ DDf1, aseq("20-39") \ DDf2, aseq("40-59") \ 
						DDf3, aseq("60-100") mcol(sourapple1)ciopts(lpatt(solid)lcol(sourapple1%50)))
						
						(DDm0, aseq("0-19") \ DDm1, aseq("20-39") \ DDm2, aseq("40-59") \ 
						DDm3, aseq("60-100") msymb(S)mcol(sourapple3)ciopts(lpatt(solid)lcol(sourapple3%50)))
						
						, 
			
			
			vertical keep(treatpost) title("") 
			yline(0, lc(sourapple4) lp(solid)) 
			ytitle("DID estimate: Clauses (`margin'; data driven)", size(medlarge)) 
			xlabel(, labsize(medium))
			xtitle("Share of women at establishment", size(medlarge))
			xlabel(none) xline(11, lp(dash)) 
			legend(ring(0) pos(2) cols(1) order(2 "Female" 4 "Male") size(medium) region(lstyle(none) color(none)))  
			plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white));
			
			graph export "$figures/female`var'_het_by_femxshare_DD.tif", replace;
			
			#delimit cr
			local ++j
		}
		
		
		/*** Heterogeneity plot ***/
		* Plot heterogeneity in treatment effect by interaction of above/below median women in estab vs. board.
		
		local depvar _clauses_DD _cl_DD_sh
		local j=1
		
		foreach var of local depvar {
		
			if `j'==1 {
				local margin = "intensive"
			}
			if `j'==2 {
				local margin = "share"
			}

			forvalues i = 0/4 {
				eststo DDf`i': reghdfe female`var' treatpost if ishare`i'==1, absorb(estab_union_pair *FE) cluster(clustergrp)
				eststo DDm`i': reghdfe male`var' treatpost if ishare`i'==1, absorb(estab_union_pair *FE) cluster(clustergrp)
			}
			
			#delimit ;
			coefplot  (DDf0, aseq("Baseline") \ DDf1, aseq("Both") \ DDf2, aseq("Workplace") \ 
						DDf3, aseq("Union") \ DDf4, aseq("Neither") mcol(sourapple1)ciopts(lpatt(solid)lcol(sourapple1%50)))
						
						(DDm0, aseq("Baseline") \ DDm1, aseq("Both") \ DDm2, aseq("Workplace") \ 
						DDm3, aseq("Union") \ DDm4, aseq("Neither") msymb(S)mcol(sourapple3)ciopts(lpatt(solid)lcol(sourapple3%50)))
						
						, 
				
			vertical keep(treatpost) title("") 
			yline(0, lc(sourapple4) lp(solid)) 
			ytitle("DID estimate: Clauses (`margin'; data driven)", size(medlarge)) 
			xlabel(, labsize(medium))
			xtitle("Below 1/3 female (workplace vs. union)", size(medlarge))
			xlabel(none) xline(11, lp(dash)) 
			legend(ring(0) pos(2) cols(1) order(2 "Female" 4 "Male") size(medium) region(lstyle(none) color(none)))  
			plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white));
			
			graph export "$figures/female`var'_het_by_femishare_DD.tif", replace;
			
			#delimit cr
			local ++j
		}
		

	
******************************************
*** UNION AFFILIATION TO CUT OVER TIME ***
******************************************

* data toggles
local CBAbefore = 2020 //2015: must negotiate CBA prior to reform; 2020: ignore restriction
local DDmethod = "base" //base: baseline method; any: clause selected in any method; all: clause selected in all methods
local geoglevel = "state_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local clstr = "employer_id" //estab_union_pair; employer_id; union_id
local cov2014 = 0 //0: allow for no CBA coverage in 2014; 1: ensure CBA coverage in 2014 
local filled = 1 //0: use new contracts panel; 1: used filled panel


	* 1 - estab-union pairs in our sample
	use "$files/estab_union_act.dta", clear
	keep if ind_mode!=. & microregion_mode!=.
	keep if treat!=. 
	keep if inrange(year, 2011, 2017)	
	keep if min_cbayear<`CBAbefore'
	if `cov2014' {
		gen xxx = (total_clauses==0)&(year==2014)
		egen nocover_bl = max(xxx), by(estab_union_pair)
		unique estab_union_pair
		unique estab_union_pair if nocover_bl==0
		keep if nocover_bl==0
		drop nocover_bl xxx
	}

	keep union_id employer_id only1union
	duplicates drop 
	

* 2 - attach info on estab workforce size
	merge m:1 employer_id using "$files/estabchars_wide.dta", keepusing(tot_employment2012 tot_fememployment2012) keep(1 3) 
	
* 3 - impute the workforce for each union 
	bys employer_id: gen unions_count = _N
	tab unions_count
	unique union_id
	unique union_id if unions_count>2
	replace tot_employment2012 = 0 if tot_employment2012==.
	replace tot_fememployment2012 = 0 if tot_fememployment2012==.
	gen tot_employment2012_imputed = tot_employment2012/unions_count
	gen tot_fememployment2012_imputed = tot_fememployment2012/unions_count

* 4 - collapse to union level
	collapse (sum) tot_employment2012 tot_fememployment2012, by(union_id)
	gen fem_share = tot_fememployment2012/tot_employment2012
	count if fem_share==.
	count
	
* 5 - merge with union central affiliation 	
	tempfile insample
	save `insample'
	
	use "$files/union_list_clean.dta", clear
	keep if union_type =="Trabalhador"  // keep only workers' unions
	keep if level == "Sindicato"		// keep local level union (do not have affiliation for other)
	keep if cs2012!=.					// keep if 2012 affiliation is not missing
	keep union_id cs* 
	
	merge 1:1 union_id using `insample', keep(3) nogen
	reshape long cs, i(union_id) j(year)

* 6. Plots
	gen CUT = (cs == 8) if cs!=. 
	tab year, gen(y_)
	label var y_1 "2012"
	label var y_2 "2013"
	label var y_3 "2014"
	label var y_4 "2015"
	label var y_5 "2016"
	
	gen zero = 0 
	label var zero "2012"
	
	*switchers
	egen xxx = max(CUT), by(union_id)
	egen yyy = min(CUT), by(union_id)
	gen switchers = (xxx!=yyy)
	egen unique_union = tag(union_id)
	tab switchers if unique_union==1

	//effect without splitting the sample
	reg CUT zero y_2 - y_5, clus(union_id)
	
	//the mean share of women workers (to split the sample)
	sum fem_share, d
	local thresh = r(mean)
	
	//figure
	reg CUT zero y_2 - y_5 if fem_share>=`thresh'  [fweight=tot_employment2012], clus(union_id)
	estimates store CUT_h
	reg CUT zero y_2 - y_5 if fem_share<`thresh' [fweight=tot_employment2012], clus(union_id)
	estimates store CUT_l
	coefplot ( CUT_h , connect(l) lpatt(solid) lcol(sourapple1)mcol(sourapple1)ciopts(lpatt(solid)lcol(sourapple1%50))) ///
	 ( CUT_l , connect(l) lpatt(solid) lcol(sourapple3)msymb(S)mcol(sourapple3)ciopts(lpatt(solid)lcol(sourapple3%50)))	 , ///
	 omitted keep(zero y_2 y_3 y_4 y_5) yline(0, lp(dash) lc(sourapple4)) ///
	 vertical ytitle("Affiliated to CUT", size(medlarge)) ///
	 yla(-0.03(0.01)0.03, labsize(medium)) xla(, labsize(medium)) ///
	 legend(order(2 "High % women" 4 "Low % women") size(medium) region(lstyle(none) color(none))) ///
	 plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white))

	 graph export "$figures/CUT_affiliation_byfemsh_unionlevW.tif", replace 
	
	

************************************************
*** TRENDS IN CLAUSES (DATA-DRIVEN APPROACH) ***
************************************************

* data toggles
local CBAbefore = 2020 //2015: must negotiate CBA prior to reform; 2020: ignore restriction
local DDmethod = "base" //base: baseline method; any: clause selected in any method; all: clause selected in all methods
local geoglevel = "state_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local clstr = "employer_id" //estab_union_pair; employer_id; union_id
local cov2014 = 0 //0: allow for no CBA coverage in 2014; 1: ensure CBA coverage in 2014 
local filled = 1 //0: use new contracts panel; 1: used filled panel


	* Filled Panel
		use "$files/filledpanel.dta", clear
		keep if ind_mode!=. & microregion_mode!=.
		keep if treat!=. 
		keep if inrange(year, 2011, 2017)	
		keep if min_cbayear<`CBAbefore'
		if `no2011' {
			drop if year==2011
			local xl = 2012
		}
		else {
			local xl = 2011
		}
		if `cov2014' {
			gen xxx = (total_clauses==0)&(year==2014)
			egen nocover_bl = max(xxx), by(estab_union_pair)
			unique estab_union_pair
			unique estab_union_pair if nocover_bl==0
			keep if nocover_bl==0
			drop nocover_bl xxx
		}

		* data driven method
		gen female_clauses_DD = female_clauses_DD`DDmethod'
		gen male_clauses_DD = male_clauses_DD`DDmethod'
		gen female_clauses_DD_b = female_clauses_DD`DDmethod'_b
		gen male_clauses_DD_b = male_clauses_DD`DDmethod'_b
		gen female_cl_DD_sh = female_cl_DD`DDmethod'_sh
		gen male_cl_DD_sh = male_cl_DD`DDmethod'_sh
		gen female_cl_DD_sh_b = female_cl_DD`DDmethod'_sh_b
		gen male_cl_DD_sh_b = male_cl_DD`DDmethod'_sh_b
		gen female_cl_DD_any = any_female_cl_DD`DDmethod'
		gen male_cl_DD_any = any_male_cl_DD`DDmethod'

		
		collapse (mean) female_clauses_DD male_clauses_DD , by(treat year)
		gen female_male_DD = female_clauses_DD/male_clauses_DD
		
			#d ;
			twoway 	(scatter female_clauses_DD year if treat == 1, lcolor(sourapple1) mcolor(sourapple1) connect(direct) sort) 
					(scatter female_clauses_DD year if treat == 0, lcolor(sourapple3) msymb(S) mcolor(sourapple2) connect(direct) sort),
					ytitle("Mean female clauses (data-driven)", size(medlarge)) ylab(, labsize(medium))
					xtitle("") xsc(r(`xl' 2017)) xlab(`xl'(1)2017, labsize(medium))
					xline(2014.5, lc(sourapple4) lpattern(dash))
					legend(ring(0) pos(11) cols(1) size(medium)
					region(lstyle(none) color(none)) order(1 "CUT" 2 "Non CUT"))
					plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white));
			graph export "$figures/female_clauses_DD_FP.tif", replace;

			twoway 	(scatter male_clauses_DD year if treat == 1, lcolor(sourapple1) mcolor(sourapple1) connect(direct) sort) 
					(scatter male_clauses_DD year if treat == 0, lcolor(sourapple3) msymb(S) mcolor(sourapple2) connect(direct) sort),
					ytitle("Mean male clauses (data-driven)", size(medlarge)) ylab(, labsize(medium))
					xtitle("") xsc(r(`xl' 2017)) xlab(`xl'(1)2017, labsize(medium))
					xline(2014.5, lc(sourapple4) lpattern(dash))
					legend(ring(0) pos(11) cols(1) size(medium)
					region(lstyle(none) color(none)) order(1 "CUT" 2 "Non CUT"))
					plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white));
			graph export "$figures/male_clauses_DD_FP.tif", replace;
			
			twoway 	(scatter female_male_DD year if treat == 1, lcolor(sourapple1) mcolor(sourapple1) connect(direct) sort) 
					(scatter female_male_DD year if treat == 0, lcolor(sourapple3) msymb(S) mcolor(sourapple2) connect(direct) sort),
					ytitle("Ratio of mean female-to-male clauses (data-driven)", size(medlarge)) ylab(, labsize(medium))
					xtitle("") xsc(r(`xl' 2017)) xlab(`xl'(1)2017, labsize(medium))
					xline(2014.5, lc(sourapple4) lpattern(dash))
					legend(ring(0) pos(11) cols(1) size(medium)
					region(lstyle(none) color(none)) order(1 "CUT" 2 "Non CUT"))
					plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white));
			graph export "$figures/female_male_DD_FP.tif", replace;
			
			#d cr

	* New Contracts
		use "$files/estab_union_act.dta", clear
		keep if ind_mode!=. & microregion_mode!=.
		keep if treat!=. 
		keep if inrange(year, 2011, 2017)	
		keep if min_cbayear<`CBAbefore'
		if `no2011' {
			drop if year==2011
			local xl = 2012
		}
		else {
			local xl = 2011
		}
		if `cov2014' {
			gen xxx = (total_clauses==0)&(year==2014)
			egen nocover_bl = max(xxx), by(estab_union_pair)
			unique estab_union_pair
			unique estab_union_pair if nocover_bl==0
			keep if nocover_bl==0
			drop nocover_bl xxx
		}

		* data driven method
		gen female_clauses_DD = female_clauses_DD`DDmethod'
		gen male_clauses_DD = male_clauses_DD`DDmethod'
		gen female_clauses_DD_b = female_clauses_DD`DDmethod'_b
		gen male_clauses_DD_b = male_clauses_DD`DDmethod'_b
		gen female_cl_DD_sh = female_cl_DD`DDmethod'_sh
		gen male_cl_DD_sh = male_cl_DD`DDmethod'_sh
		gen female_cl_DD_sh_b = female_cl_DD`DDmethod'_sh_b
		gen male_cl_DD_sh_b = male_cl_DD`DDmethod'_sh_b
		gen female_cl_DD_any = any_female_cl_DD`DDmethod'
		gen male_cl_DD_any = any_male_cl_DD`DDmethod'

		
		collapse (mean) female_clauses_DD male_clauses_DD, by(treat year)
		gen female_male_DD = female_clauses_DD/male_clauses_DD
		
		
	#d ;
	twoway 	(scatter female_clauses_DD year if treat == 1, lcolor(sourapple1) mcolor(sourapple1) connect(direct) sort) 
			(scatter female_clauses_DD year if treat == 0, lcolor(sourapple3) msymb(S) mcolor(sourapple3) connect(direct) sort),
			ytitle("Mean female clauses (data-driven)", size(medlarge)) ylab(2(0.5)4, labsize(medium) gmax gmin)
			xtitle("") xsc(r(`xl' 2017)) xlab(`xl'(1)2017, labsize(medium))
			xline(2014.5, lc(sourapple4) lpattern(dash))
			legend(ring(0) pos(11) cols(1) size(medium)
			region(lstyle(none) color(none)) order(1 "CUT" 2 "Non CUT"))
			plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white));
	graph export "$figures/female_clauses_DD.tif", replace;

	#d cr


*************************************************************
*** IMPACT ON GENDER REPRESENTATION IN LOCAL UNION BOARDS ***
*************************************************************

* data toggles
local CBAbefore = 2020 //2015: must negotiate CBA prior to reform; 2020: ignore restriction
local DDmethod = "base" //base: baseline method; any: clause selected in any method; all: clause selected in all methods
local geoglevel = "state_mode" //state_mode: for state-yr FEs; microregion_mode: for microregion-yr FEs; stateind: for state-industry-yr FEs; microind: for microregion-industry-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local clstr = "employer_id" //estab_union_pair; employer_id; union_id
local cov2014 = 0 //0: allow for no CBA coverage in 2014; 1: ensure CBA coverage in 2014 
local filled = 1 //0: use new contracts panel; 1: used filled panel


	if `filled' {
		use "$files/filledpanel.dta", clear
	}
	else{
		use "$files/estab_union_act.dta", clear
	}
	keep if ind_mode!=. & microregion_mode!=.
	keep if treat!=. 
	keep if inrange(year, 2011, 2017)	
	keep if min_cbayear<`CBAbefore'
	if `cov2014' {
		gen xxx = (total_clauses==0)&(year==2014)
		egen nocover_bl = max(xxx), by(estab_union_pair)
		unique estab_union_pair
		unique estab_union_pair if nocover_bl==0
		keep if nocover_bl==0
		drop nocover_bl xxx
	}
	keep union_id 
	duplicates drop
	tempfile union
	save `union'

	// event study variables
	use "$files/union_boards_panel_20112019", clear
	merge m:1 union_id using `union'
	drop if _merge==2
	gen incba_sample_alt = (_merge==3)
	drop _merge
	
	tab year, gen(y_)
	local year = 2011 
	forvalues y = 1/9 {
		gen int_`y' = y_`y'*treat
		label var int_`y' "`year'"
		local ++year
	}

	rename 	int_4 zero 
	replace zero = 0
	gen treatpost = (treat==1)&(year>=2015)

	// dummy for female president of VP
	egen female_PorVP = rowmax(female_president female_vicepresident)
	
	***  Effect of quota on board composition (first stage) - unit of analysis = union
	****************************************************************************

		if `no2011' {

		drop if year==2011

	*** Sample: Subset of unions negotiating CBAs in our sample
		** Effect on share female
		reghdfe sh_female treatpost if incba_sample_alt==1, absorb(union_id_num year) clus(union_id_num)
			local coeff = _b[treatpost]
			local coeff : di %04.3f `coeff'
			local serr = _se[treatpost]
			local serr : di %04.3f `serr'
		sum sh_female if year==2014 & treat==1 & incba_sample_alt==1
		
		reghdfe sh_female y_2 - y_9 int_1 - int_9 if incba_sample_alt==1, absorb(union_id_num) clus(union_id_num) nocons
		estimates store sh_female
		coefplot (sh_female , connect(l) lpatt(solid)lcol(sourapple1)mcol(sourapple1)ciopts(lpatt(solid)lcol(sourapple1%50)) ///
		text(-0.005 5 "`coeff'" "(`serr')", color(sourapple1) size(medium) placement(se))) , ///
		omitted keep(int_2 int_3 zero int_5 int_6 int_7 int_8 int_9) yline(0, lp(solid) lcol(sourapple4)) ylab(-0.01(0.01)0.02) ///
		xline(3.5, lp(dash) lcol(sourapple4)) vertical ///
		ytitle("Share of women in union board", size(medlarge)) ylab(-0.02(0.01)0.02, labsize(medium) gmax gmin) ///
		xla(, labsize(medium)) ///
		plotregion(fcolor(white)) graphregion(fcolor(white)) graphregion(color(white))
		graph export "$figures/shfem_unionboards_did_restr.tif", replace 
		
		** Effect on female president or VP
		reghdfe female_PorVP treatpost if incba_sample_alt==1, absorb(union_id_num year) clus(union_id_num)
			local coeff = _b[treatpost]
			local coeff : di %04.3f `coeff'
			local serr = _se[treatpost]
			local serr : di %04.3f `serr'
		sum female_PorVP if year==2014 & treat==1 & incba_sample_alt==1
		
		reghdfe female_PorVP  y_2 - y_9 int_1 - int_9 if incba_sample_alt==1, absorb(union_id_num) clus(union_id_num) nocons
		estimates store female_PorVP
		coefplot (female_PorVP , connect(l) lpatt(solid)lcol(sourapple1)mcol(sourapple1)ciopts(lpatt(solid)lcol(sourapple1%50)) ///
		text(0.03 5 "`coeff'" "(`serr')", color(sourapple1) size(medium) placement(se))) , ///
		omitted keep(int_2 int_3 zero int_5 int_6 int_7 int_8 int_9) yline(0, lp(solid) lcol(sourapple4)) ///
		xline(3.5, lp(dash) lcol(sourapple4)) vertical ///
		ytitle("Prob. woman president or vice-president", size(medlarge)) ylab(-0.04(0.02)0.04, labsize(medium) gmax gmin) ///
		xla(, labsize(medium)) ///
		graphregion(color(white))
		graph export "$figures/womPorVP_unionboards_did_restr.tif", replace 
		
		}
		
		

log close
